package com.sphd.beautysalon.activitys.kuncun;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.sphd.beautysalon.info.LoginInfo;
import com.sphd.beautysalon.R;
import com.sphd.beautysalon.adapters.MyViewPagerAdapter;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.fragments.goods.PutInFragment;
import com.sphd.beautysalon.fragments.goods.PutOutFragment;
import com.sphd.beautysalon.fragments.goods.SellFragment;
import com.sphd.beautysalon.info.InorOutRecordInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.utils.Constant;
import com.wzh.frame.utils.TimeUtils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 单个商品的出入库记录展示
 */
public class GoodsActivity extends BaseActivity {
    Intent intent;
    Bundle bundle;
    @BindView(R.id.choose_time)
    TextView chooseTime;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.m_vp)
    ViewPager mVp;
    private RestaurantInfo restaurantInfo;
    private LoginInfo loginInfo;
    private InorOutRecordInfo inorOutRecordInfo;
    String[] titles = {"入库", "出库", "销售"};
    List<Fragment> fragments;
    MyViewPagerAdapter myVPAdapter;
    PutInFragment putInFragment;
    PutOutFragment putOutFragment;
    SellFragment sellFragment;
    String  day;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        inorOutRecordInfo = (InorOutRecordInfo) bundle.getSerializable(Constant.INOROUTRECODR);
        loginInfo = DataSupport.findLast(LoginInfo.class);
        setTitleBar(toolbar,inorOutRecordInfo.getDishes_name());
        day= TimeUtils.getCurrentTimeInString(TimeUtils.DATE_FORMAT_DATE);
        fragments = new ArrayList<>();

        putInFragment = new PutInFragment();

        putOutFragment = new PutOutFragment();
        sellFragment = new SellFragment();



        initTabAndVp();
    }

    private void initTabAndVp() {
        fragments.add(putInFragment);
        fragments.add(putOutFragment);
        fragments.add(sellFragment);

        putInFragment.setInfo(restaurantInfo,inorOutRecordInfo,day);
        putOutFragment.setInfo(restaurantInfo,inorOutRecordInfo,day);
        sellFragment.setInfo(restaurantInfo,inorOutRecordInfo,day);
        myVPAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), fragments, titles);
        mVp.setAdapter(myVPAdapter);
        tabs.setupWithViewPager(mVp);
        mVp.setOffscreenPageLimit(3);
    }

    @OnClick(R.id.choose_time)
    public void onClick() {
        showTimedialog();
    }

    private void showTimedialog() {
        LayoutInflater li = LayoutInflater.from(this);
        View promptsView = li.inflate(R.layout.layout_start_end_time, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                this);
        alertDialogBuilder.setView(promptsView);
        final DatePicker startDatePicker = (DatePicker) promptsView
                .findViewById(R.id.start_datePicker);
        TextView startTitle = (TextView) promptsView.findViewById(R.id.start_title);

        TextView endTitle = (TextView) promptsView.findViewById(R.id.end_title);
        endTitle.setVisibility(View.GONE);
        final DatePicker endDatePicker = (DatePicker) promptsView
                .findViewById(R.id.end_datePicker);
        endDatePicker.setVisibility(View.GONE);
        startDatePicker.init(startDatePicker.getYear(),  startDatePicker.getMonth(), startDatePicker.getDayOfMonth(), new DatePicker.OnDateChangedListener() {

            public void onDateChanged(DatePicker view, int year, int monthOfYear,
                                      int dayOfMonth) {
                Calendar c = Calendar.getInstance();
                c.set(year, monthOfYear, dayOfMonth);
                endDatePicker.init(startDatePicker.getYear() + 1, startDatePicker.getMonth(), startDatePicker.
                        getDayOfMonth(), dateSetListener);
            }
        });

        alertDialogBuilder.setTitle("编辑查询时间");
        // set dialog message
        alertDialogBuilder
                .setCancelable(false)
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                day = startDatePicker.getYear() + "-" +
                                        ((startDatePicker.getMonth() + 1) < 10 ? "0" +
                                                (startDatePicker.getMonth() + 1) :
                                                (startDatePicker.getMonth() + 1))
                                        + "-" + ((startDatePicker.getDayOfMonth() + 1) < 10 ? "0" +
                                        startDatePicker.getDayOfMonth() :
                                        startDatePicker.getDayOfMonth());
//                                goodsList.clear();
                                chooseTime.setText(day);
                                putInFragment.setInfo(restaurantInfo,inorOutRecordInfo,day);
                                putOutFragment.setInfo(restaurantInfo,inorOutRecordInfo,day);
                                sellFragment.setInfo(restaurantInfo,inorOutRecordInfo,day);



                                if (mVp.getCurrentItem()==0){
                                    putInFragment.onRefresh();
                                }else if (mVp.getCurrentItem()==1){
                                    putOutFragment.onRefresh();
                                }else if (mVp.getCurrentItem()==2){
                                    sellFragment.onRefresh();
                                }
                                Log.e("tabs",mVp.getCurrentItem()+"");
                            }
                        })
                .setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }

    private DatePicker.OnDateChangedListener dateSetListener = new DatePicker.OnDateChangedListener() {

        public void onDateChanged(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {
            Calendar c = Calendar.getInstance();
            c.set(year, monthOfYear, dayOfMonth);
            System.out.println("TEST");

        }
    };
}
